/*
 * DA2.c
 *
 * Created: 3/3/2014 9:58:01 AM
 *  Author: hillm3
 */ 


#include <avr/io.h> // input/output for avr
#include <avr/interrupt.h> // interrupt stuff for avr

#define F_CPU 8000000UL  // Define cpu clock rate so delay can be known
                                      
#include <util/delay.h> // delay times for avr

int counterInt = 0; // Holds how many global interrupts happened

ISR(INT0_vect){
	// This happens every 20 cycles. It happens on the falling edge. So 10 to get high, then 10 to get fall.
	// From main() and which the wire connects from PD2(Int0) to PD6. PD6 outputs every 10 cycles.


	
	counterInt++; // Increment how many falling edges happen
	//PORTD = 0b11111111;
	if(counterInt==2){
		///Once two falling edges happen
	// This occurs every 40 cycles
	PORTD ^= 1 << 7; /// Toggle   bit 7
	counterInt = 0; // Set back to 0.
	
	}
	

	

}





int main(void)
{
	


	DDRB = 0b11111111;///Sets the direction Used for LED bits 0-7
	DDRD = 0b11000000;///Sets the direction. We use bit 6 and 7 of port D. bit 6 is every 10 cycles, bit 7 is every 40 cycles. Bit 2 is the interrupt reader
    PORTB = 0b00000000; // Make sure PORTB is cleared to start
	PORTD = 0b00000000; //  Make sure PORTD Is cleared to start
	int everyten = 0; // Used to count for every 10 cycle.
	int counter = 0; // Used to hold the counter count
			EICRA = 0b00000010; // Sets the interrupt for falling edge.
					EIMSK=1; // Enables the interrupt
		sei(); // Enable the interrupt


		
	
	while(1)
	{

		_delay_ms(250); // Set Delay 0.25 seconds. At end, set to high. At end is rising edge.


		counter++;// Increment the counter
		PORTB = counter; // Update portb to the new counter values
		
		everyten++;// Increment once
		if(everyten==10){ // Hit 10
			
		everyten = 0; // Set back to 0
			PORTD ^= 1 << 6; /// Toggle bit 6
			
			
		}
		

		_delay_ms(250); // Delay 0.25 seconds. At end, set to low. At end is falling edge

		
		


	}
	
}
